package com.example.springbootknife4j.controller;

import com.example.springbootknife4j.Result;
import com.example.springbootknife4j.vo.Course;
import com.example.springbootknife4j.vo.User;
import com.example.springbootknife4j.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.UUID;

/**
 * TODO
 *
 * @author 发哥讲Java
 * @version 1.0
 * @date 2021-05-27 15:12
 */
@RestController
// 给整个类定义 tags, 将所有接口归档当前tags(菜单)下
// 给controller 起别名 类注解
@Api(tags = "测试API类 - hello")
public class HelloController {

    // 方法的描述,
    @GetMapping("/hello")
    @ApiOperation(value = "访问hello接口")
    @ApiOperationSupport(author = "发哥讲Java")//标注作者
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "age", value = "年龄"),
    })
    public Result<String> hello(String name, Integer age) {
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        return Result.ok(name + "--" + age);
    }

    // 方法的描述,
    @GetMapping("/helloVo")
    @ApiOperation(value = "访问复杂参数-hello接口")
    @ApiOperationSupport(author = "发哥讲Java")//标注作者
    public Result<String> helloVo(UserVo userVo) {
        System.out.println("userVo = " + userVo);
        return Result.ok(userVo.toString());
    }

    @PostMapping("/post")
    @ApiOperation("复杂post请求接口")
    @ApiOperationSupport(author = "发哥讲Java")//标注作者
    public Result<User> postMapping(@RequestBody UserVo userVo) {
        // 构建user
        User user = new User();
        user.setId(UUID.randomUUID().toString())
                .setUsername(userVo.getUsername())
                .setPassword(userVo.getPassword())
                .setCrateTime(userVo.getCrateTime());
        // 构建 课程list
        Course courseVo = new Course();
        courseVo.setCourseId(UUID.randomUUID().toString()).setName("");
        user.setCourseList(Collections.singletonList(courseVo));
        return Result.ok(user);
    }

}
